<?php
defined( "_SBKH_" ) or die( 'ERROR : RESTRICT ACCESS THIS LOCATION.' );
/*
@Author: Sam Sambo, Khmer
@Date: 2012-12-02
@SBKH Framework  
*/	
require_once( ROOT . 'config/Database.class.php' );
class SBKH extends Database{
	private $_layout = "";
	private $_ext = "";	
	private $_pageTitle = "";
	private $_urlTitle = "";
	private $_navigation = "";
	private $_navigationName = "";
	private $_controller = "";
	private $_view = "";
	private $_content = "";
	private $_baseURL = BASE_URL;
	private $_params = array();
	
	public function __construct(){
		$this -> _layout = "index";
		$this -> _ext = "php";
		$this -> _baseURL = BASE_URL;
		$this -> _pageTitle = "Home";
		$this -> _urlTitle = "Home";
		$this -> _controller = "sample";
		$this -> _content = "";
		$this -> _navigation = "";
		$this -> _navigationName = "";
	}	
	/*
	** Function data() to return data after submit
	*/
	public function Data(){
		//return $_POST['data'];
		foreach($_POST['data'] as $table => $fields){
			foreach($fields as $field => $value){
				if(!isset($this -> Data[$table][$field])){
					$this -> Data[$table][$field] = $value;
				}
			}
		}
		return $this -> Data;
	}	
	public function SetBaseURL($baseURL = ""){
		$this -> _baseURL = $baseURL;	
	}
	public function GetBaseURL(){
		return $this -> _baseURL;	
	}
	public function SetURLTitle($urlTitle = ""){
		$this -> _urlTitle = $urlTitle;	
	}
	public function GetURLTitle(){
		return $this -> _urlTitle;	
	}
	public function SetPageTitle($title = ""){
		$this -> _pageTitle = $title;	
	}
	public function GetPageTitle(){
		return $this -> _pageTitle;	
	}
	public function SetNavigation($navigation = ""){// Navagiation could be Left or Right side of body page
		$this -> _navigation = $navigation;	
	}
	public function GetNavigation(){
		return $this -> _navigation;	
	}
	public function SetNavigationName($navigation = ""){// Navagiation could be Left or Right side of body page
		$this -> _navigationName = $navigation;
	}
	public function GetNavigationName(){
		return $this -> _navigationName;
	}
	public function SetContent($content = ""){
		$this -> _content = $content;	
	}
	public function GetContent(){
		return $this -> _content;	
	}
	public function SetController($controller = ""){
		$this -> _controller = $controller;	
	}
	public function GetController(){
		return $this -> _controller;	
	}
	public function SetViewName($view = ""){
		$this -> _view = $view;	
	}
	public function GetViewName(){
		return $this -> _view;	
	}
	public function SetLayout( $layout = "default", $ext = "php" ){
		$this -> _layout = $layout;
		$this -> _ext = $ext;
	}
	public function GetLayout(){
		return $this -> _layout;	
	}
	public function SetVar(){		
		$params = func_get_args();
		$i = 0;
		for($i = 0; $i < count($params); $i+=2){ 
			$this -> _params[] = array($params[$i] => $params[$i+1]);
			if($i+2 > count($params) -2){
				break;
			}			
		}
	}
	public function GetExt(){
		return $this -> _ext;
	}
	public function GetVar(){
		return $this -> _params;
	}
	/*
	** Function view() to read any file which passed from controller
	** Parameter: name of view
	*/
	public function View($view = "", $nav_name = "", $help = false){		
		if($view != ""){	
			$this -> SetViewName(str_replace('-','_', $view));
			if($help){
				$_skh_view = VIEW  . DS . str_replace('-','_', $view) . DOT . $this -> _ext;
			}else{
				$_skh_view = VIEW . str_replace('-','_', getController()) . DS . str_replace('-','_', $view) . DOT . $this -> _ext;
				if(!file_exists($_skh_view)){
					$_skh_view = VIEW . str_replace('-','_', getController()) . 's' . DS . str_replace('-','_', $view) . DOT . $this -> _ext;
				}
				if(!file_exists($_skh_view)){
					$_skh_view = VIEW . str_replace('-','_', getController()) . 'es' . DS . str_replace('-','_', $view) . DOT . $this -> _ext;	
				}
				if(!file_exists($_skh_view)){
					$_skh_view = VIEW . str_replace('-','_', getController()) . 'ies' . DS . str_replace('-','_', $view) . DOT . $this -> _ext;
				}
			}
			if($nav_name !== ""){
				$this -> SetNavigationName($nav_name);
				$_skh_nav_menu = ELEMENT . DS . str_replace('-','_', $nav_name) . DOT . $this -> _ext;
				if(file_exists($_skh_nav_menu)){
					$nav_menu = str_replace('<?=', '<?php echo ', file_get_contents($_skh_nav_menu));
					$nav_menu = preg_replace("/;*\s*\?>/", "; ?>", $nav_menu);						
					$this -> SetNavigation( $nav_menu);										
				}else{			
					$content = "ERROR: Navigation Menu [ " . $_skh_nav_menu . "] Not Found!!!";
					$this -> SetNavigation($content);
				}
			}
			if(file_exists($_skh_view)){
				$content = str_replace('<?=', '<?php echo ', file_get_contents($_skh_view));
				$content = preg_replace("/;*\s*\?>/", "; ?>", $content);			
				$all_varialbes = $this -> GetVar();// Get all variable which assign from controller
			//	pr($all_varialbes);die();
				foreach($all_varialbes as $all_varialbe){
					foreach($all_varialbe as $variable_name => $value){
						$$variable_name = $value;
					}
				}
				$this -> SetContent( $content);										
			}else{
				$_skh_view = VIEW . 'errors/error_view.php';						
				$this -> SetContent(file_get_contents($_skh_view));
			}
		}else{
			$_skh_view = VIEW . 'errors/error.404.html';				
			$this -> SetContent(file_get_contents($_skh_view));
		}
		$this -> Render();
	}
	/*
	** Function render() to translate PHP file and HTML code to output on the browser
	** Parameter is optional, can be passed from controller when we don't need call view() function
	*/
	public function Render($content = ""){
		if($content != ""){
			$this -> SetContent($content);
		}
		$all_varialbes = $this -> GetVar();	
		foreach($all_varialbes as $all_varialbe){
			foreach($all_varialbe as $variable_name => $value){
				$$variable_name = $value;
			}
		}
		if($this -> _layout){
			$_skh_layout = LAYOUT . $this -> _layout . DOT . $this -> _ext;
			if(file_exists($_skh_layout)){
				$content = str_replace('<?=', '<?php echo ', file_get_contents($_skh_layout));
				$content = preg_replace("/;*\s*\?>/", "; ?>", $content);
				eval("?>$content");
			}else{	
				$this -> SetURLTitle('SBKH:: Sorry layout not found!!!');
				$this -> SetVar('layout', $this -> GetLayout());
				$this -> SetLayout('error');				
				$this -> view('errors/error-layout',true);
			}
		}elseif($this -> _layout == false){
			$this -> SetLayout('');
			eval('?>' . $this -> GetContent());
			exit;
		}
	}
	public static function GetHelper( $element = "", $ext = "php" ){
		require_once HELPER . str_replace('-','_', $element) . DOT . $ext;
	}
	public static function GetElement( $element = "", $ext = "php" ){
		require_once ELEMENT . str_replace('-','_', $element) . DOT . $ext;
	}
	public static function EetElementParent( $element = "", $ext = "php" ){
		require_once  "../" . ELEMENT . str_replace('-','_', $element) . DOT . $ext;
	}
	public static function GetView( $view_name = "", $ext = "php" ){
		require_once VIEW . str_replace('-','_', $view_name) . DOT . $ext;
	}
	public static function GetVendor( $filename = "", $ext = "php" ){
		require_once VENDOR . str_replace('-','_', $filename) . DOT . $ext;
	}
	public static function LoadModel( $model_name = "", $ext = "php" ){
		require_once MODEL . ucwords($model_name) . "Model" . DOT . $ext;
		$model_name .= "Model";
		return new $model_name();
		
	}
	public static function Redirect( $url = '', $isThisLink = false ){
		if(getController() == "")
			header('location:' . BASE_URL . $url);
		else 
			if($isThisLink)
				header('location:' . BASE_URL . $url);
			else
				header('location:' . BASE_URL . getController() . DS. $url);
	}
}
?>